package com.dsc.shoppcar.servlet;

import com.dsc.shoppcar.dao.DataMoniDao;
import com.dsc.shoppcar.pojo.Goods;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author: Du Shun Chang
 * @title: IndexServlet
 * @date: 2022/4/14 8:40
 * @description: TODO
 * @version：1.0
 */
public class IndexServlet extends HttpServlet {

//    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        StringBuffer sf = new StringBuffer();
        sf.append("<html>");
        sf.append("<head>");
        sf.append("<title>商品选购页面</title>");
        sf.append("<script type=\"text/javascript\">");
        sf.append("function suc(){alert(\"添加成功！\")}");
        sf.append("</script>");
        sf.append("</head>");
        sf.append("<body>");
        sf.append("<h2>这里是商品选购页面</h2>");
        sf.append("<table border='1'>");
        sf.append("<tr>");
        sf.append("<td>商品编号</td><td>商品名</td><td>图片</td><td>价格</td><td>操作</td>");
        sf.append("</tr>");

        List<Goods> list = DataMoniDao.getData();
        for(int i = 0 ; i < list.size(); i ++){
            Goods goods = list.get(i);
            sf.append("<tr>");
            sf.append("<td>" + goods.getId() + "</td>");
            sf.append("<td>" + goods.getName() + "</td>");
            sf.append("<td><img src=\"" + goods.getImg_url() + "\" height=\"60px\"></td>");
            sf.append("<td>" + goods.getPrice() + "</td>");
            sf.append("<td><a onclick=\"suc()\" href=\"cart?id=" + goods.getId() + "\">加入购物车</a>&nbsp;&nbsp;<a href=\"cart\">直接购买</a></td>");
            sf.append("</tr>");
        }
        sf.append("</table>");
        sf.append("<a href=\"look\">查看我的购物车</a>");
        sf.append("</body>");
        sf.append("</html>");
        out.write(sf.toString());
        out.close();
    }
}
